package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserPswActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_psw_confirm)
    EditText etPswConfirm;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_psw);
        ButterKnife.bind(this);
        initView();
    }
    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("设置登录密码");
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                checkInputDatas();
                break;
        }
    }

    private void checkInputDatas() {
        String psw=etPsw.getText().toString();
        String pswConfirm=etPswConfirm.getText().toString();
        if(TextUtils.isEmpty(psw)){
            ToastUtils.toastForShort(this,"密码不能为空");
            return;
        }
        if(TextUtils.isEmpty(pswConfirm)){
            ToastUtils.toastForShort(this,"确认密码不能为空");
            return;
        }
        if(TextUtils.equals(psw,pswConfirm)){
            ToastUtils.toastForShort(this,"2次输入密码不一致");
            return;
        }
        confirm(psw);
    }

    private void confirm(String psw) {
        loadingDialog.show();
        OkgoUtil.post(Http.setUserPas, this)
                .params("password", psw)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(UserPswActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                        } else {
                        }
                        ToastUtils.toastForShort(UserPswActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                    }
                });
    }

}
